
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.EmployeeBOInterface;
import org.cuatrovientos.erp.model.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * EmployeeController
 * @author CaselHunter
 */
@Controller
public class EmployeeController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(EmployeeController.class);

	@Autowired
	private EmployeeBOInterface employeeBO;

	/**
	 * default constructor
	 */
	public EmployeeController () {
		logger.info("Employee BEAN instantiated");
	}

	/**
	 * handles default /employee or /employee/list request
	 * @return
	 */
	@RequestMapping({"/employee","/employee/list"})
	public ModelAndView employeeDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Employee> employeeList = employeeBO.selectAll();
		model.put("cusmtomers", employeeList );
		
		return new ModelAndView("EmployeeList", model);     
	}


	/**
	 * handles default /employee/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/employee/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Employee> employeeList = employeeBO.search(name);
		model.put("employees", employeeList);
		
		return new ModelAndView("EmployeeList", model);     
	}

	/**
	 * handles default /employee/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/employee/delete"})
	public String deleteEmployee(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		employeeBO.deleteById(id);
		
		return "redirect:/employee";
	}

	/**
	 * handles default /employee/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/employee/new"})
	public ModelAndView newEmployee() {
		logger.info("new employee");
		
		Employee employee = new Employee();

		return new ModelAndView("NewEmployee", "employee", employee);
	}

	/**
	 * handles default /employee/save request
	 * @param employee
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/employee/save"})
	public String newEmployee(@Valid Employee employee, BindingResult bindingResult) {
		logger.info("new employee " + employee.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("employee", employee);
			
			return "NewEmployee";
		}

		employeeBO.insert(employee);
		
		return "redirect:/employee";
	}

	/**
	 * handles default /employee/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/employee/detail"})
	public ModelAndView employeeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Employee employee = employeeBO.selectById(id);
		model.put("employee", employee);

		return new ModelAndView("EmployeeDetail", model);
	}


	/**
	 * handles default /employee/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/employee/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Employee employee = employeeBO.selectById(id);
		model.put("employee", employee);
		
		return new ModelAndView("UpdateEmployee",model);
	}


	/**
	 * handles default /employee/saveupdate request
	 * @param employee
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/employee/saveupdate"})
	public String saveUpdate(@Valid Employee employee, BindingResult bindingResult) {
		logger.info("update employee " + employee.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("employee", employee);
			
			return "UpdateEmployee";
		}

		employeeBO.update(employee);
		
		return "redirect:/employee";
	}
	
}
